package com.zcx.qshop.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ListView;

import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilApp;
import com.zcx.qshop.R;
import com.zcx.qshop.QSApplication;
import com.zcx.qshop.adapter.MyOrderAdapter;
import com.zcx.qshop.conn.JsonOrderAsyGet;
import com.zcx.qshop.conn.JsonOrderSureAsyGet;
import com.zcx.qshop.conn.JsonOrderdeleteAsyGet;
import com.zcx.qshop.dialog.WhetherDialog;
import com.zcx.qshop.pay.PayAction;
import com.module.pull.PullToRefreshBase;
import com.module.pull.PullToRefreshListView;
import com.zcx.qshop.view.OrderBarView;
import com.zcx.qshop.view.TitleView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2015/8/19 0019.
 */
public class MyOrderActivity extends QSActivity {

    public static OnOrderChangeListener onOrderChangeListener;

    @BoundView(R.id.title_view) private TitleView titleView;

    @BoundView(R.id.my_order_bar_view) private OrderBarView orderBarView;

    @BoundView(R.id.my_order_list_view) private PullToRefreshListView pullToRefreshListView;

    private PullToRefreshBase.OnRefreshListener<ListView> onRefreshListener;

    private List<JsonOrderAsyGet.Info.Order> list = new ArrayList<>();

    private MyOrderAdapter adapter;

    private JsonOrderAsyGet.Info info;

    private JsonOrderAsyGet jsonOrderAsyGet = new JsonOrderAsyGet(QSApplication.QSPreferences.readUid(), "", "", new AsyCallBack<JsonOrderAsyGet.Info>(){

        private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");

        @Override
        public void onSuccess(String toast, int type, JsonOrderAsyGet.Info info) throws Exception {

            if(type == 0){

                list.clear();

            }

            list.addAll((MyOrderActivity.this.info = info).orders);

            adapter.notifyDataSetChanged();

        }

        @Override
        public void onEnd(String toast, int type) throws Exception {

            pullToRefreshListView.setLastUpdatedLabel(simpleDateFormat.format(new Date(System.currentTimeMillis())));

            pullToRefreshListView.onPullUpRefreshComplete();

            pullToRefreshListView.onPullDownRefreshComplete();

        }

    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_my_order);

        titleView.showStyle(TitleView.Style.FULL);

        titleView.showType(TitleView.Type.BACK_TWO, TitleView.Type.NAME_ONE);

        titleView.setTitleName("我的订单");

        titleView.setOnTitleItemClickListener(new TitleView.OnTitleItemClickListener() {

            @Override
            public void onBackClick() {

                finish();

            }

        });

        pullToRefreshListView.setPullRefreshEnabled(true);

        pullToRefreshListView.setScrollLoadEnabled(true);

        pullToRefreshListView.setPullLoadEnabled(false);

        pullToRefreshListView.setOnRefreshListener(onRefreshListener = new PullToRefreshBase.OnRefreshListener<ListView>() {

            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

                jsonOrderAsyGet.page = "1";

                jsonOrderAsyGet.execute(MyOrderActivity.this, false, 0);

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

                if (info != null && info.ispage.equals("1")) {

                    jsonOrderAsyGet.page = info.nextpage;

                    jsonOrderAsyGet.execute(MyOrderActivity.this, false, 1);

                } else {

                    pullToRefreshListView.onPullUpRefreshComplete();

                    pullToRefreshListView.onPullDownRefreshComplete();

                }

            }

        });

        ListView listView = pullToRefreshListView.getRefreshableView();

        listView.setDivider(null);

        listView.setVerticalScrollBarEnabled(false);

        listView.setAdapter(adapter = new MyOrderAdapter(this, list) {

            private PayAction payAction = new PayAction(MyOrderActivity.this) {

                @Override
                protected void onSuccess() {

                    startVerifyActivity(CompleteOrderActivity.class);

                    onRefreshListener.onPullDownToRefresh(null);

                }

                @Override
                protected void onEnd() {


                }

            };

            private JsonOrderdeleteAsyGet jsonOrderdeleteAsyGet = new JsonOrderdeleteAsyGet(QSApplication.QSPreferences.readUid(), "", new AsyCallBack() {

                @Override
                public void onSuccess(String toast, int type, Object o) throws Exception {

                    adapter.remove(currentOrder);

                }

            });

            private JsonOrderSureAsyGet jsonOrderSureAsyGet = new JsonOrderSureAsyGet(QSApplication.QSPreferences.readUid(), "", new AsyCallBack() {

                @Override
                public void onSuccess(String toast, int type, Object o) throws Exception {

                    onRefreshListener.onPullDownToRefresh(null);

                }

            });

            private JsonOrderAsyGet.Info.Order currentOrder;

            @Override
            protected void onDetail(JsonOrderAsyGet.Info.Order order) {

                startVerifyActivity(OrderDetailActivity.class, new Intent().putExtra("Order", order));

            }

            @Override
            protected void onRefund(JsonOrderAsyGet.Info.Order order) {

                new WhetherDialog(MyOrderActivity.this, "退款请拨打电话联系客服") {

                    @Override
                    protected void onYes() {

                        UtilApp.call(MyOrderActivity.this, getResources().getString(R.string.app_call));

                    }

                }.show();

            }

            @Override
            protected void onReceipt(final JsonOrderAsyGet.Info.Order order) {

                new WhetherDialog(MyOrderActivity.this, "是否确认收货") {

                    @Override
                    protected void onYes() {

                        jsonOrderSureAsyGet.orderid = order.id;

                        jsonOrderSureAsyGet.execute(MyOrderActivity.this);

                    }

                }.show();

            }

            @Override
            protected void onDelete(final JsonOrderAsyGet.Info.Order order) {

                new WhetherDialog(MyOrderActivity.this, "是否删除订单") {

                    @Override
                    protected void onYes() {

                        jsonOrderdeleteAsyGet.orderid = (currentOrder = order).id;

                        jsonOrderdeleteAsyGet.execute(MyOrderActivity.this);

                    }

                }.show();

            }

            @Override
            protected void onAssess(JsonOrderAsyGet.Info.Order order) {

                startVerifyActivity(CommodityAssessActivity.class, new Intent().putExtra("Order", order));

            }

            @Override
            protected void onPay(final JsonOrderAsyGet.Info.Order order) {

                new WhetherDialog(MyOrderActivity.this, "是否支付当前订单") {

                    @Override
                    protected void onYes() {

                        try {

                            payAction.pay(order.ordernum, QSApplication.QSPreferences.readUid(), order.ordernum, order.account);

                        } catch (Exception e) {

                            e.printStackTrace();

                        }

                    }

                }.show();

            }

        });

        orderBarView.setOnItemClickListener(new OrderBarView.OnItemClickListener() {

            @Override
            public void onItemClick(String type) {

                list.clear();

                adapter.notifyDataSetChanged();

                jsonOrderAsyGet.type = type;

                jsonOrderAsyGet.page = "1";

                jsonOrderAsyGet.execute(MyOrderActivity.this, true, 1);

            }

        });

        onOrderChangeListener = new OnOrderChangeListener() {

            @Override
            public void onOrderChange() {

                jsonOrderAsyGet.page = "1";

                jsonOrderAsyGet.execute(MyOrderActivity.this);

            }

        };

    }

    public interface OnOrderChangeListener{

        void onOrderChange();

    }

}
